package com.adee.nowcoder.huawei;

import java.util.Scanner;

public class T039_HJ039 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String mask = in.nextLine();
            String ip1 = in.nextLine();
            String ip2 = in.nextLine();
            if(!ismask(mask) || !isip(ip1) || !isip(ip2)) {
                System.out.println(1);
                continue;
            }
            if((toInt(mask) & toInt(ip1)) == (toInt(mask) & toInt(ip2))) {
                System.out.println(0);
            } else {
                System.out.println(2);
            }
        }
    }

    public static boolean isip(String ip) {
        if(ip != null && ip.length() > 0) {
            String[] arr = ip.split("\\.");
            if(arr.length == 4) {
                for(int i = 0; i < arr.length; i++) {
                    int a = Integer.parseInt(arr[i]);
                    if(a < 0 || a > 255) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }
    public static boolean ismask(String m) {
        if(isip(m)) {
            int a = toInt(m);
            String bs = Integer.toBinaryString(a);
            int i = bs.indexOf("1");
            int j = bs.lastIndexOf("1");
            if(i == 0 && j < bs.length()-1) {
                String sub = bs.substring(0, j+1);
                if(sub.indexOf("0") < 0) {
                    return true;
                }
            }
        }
        return false;
    }
    public static int toInt(String ip) {
        String[] arr = ip.split("\\.");
        return (Integer.parseInt(arr[0]) << 24)
                + (Integer.parseInt(arr[1]) << 16)
                + (Integer.parseInt(arr[2]) << 8)
                + Integer.parseInt(arr[3]);
    }
}
